/**
 * Created by zhoulin on 2019/4/3.
 */
var companyId = sessionStorageUtils.getJson(storageKey.sessionStorageKey.companyId);
var schoolId = sessionStorageUtils.getJson(storageKey.sessionStorageKey.schoolId);
var schoolMap = sessionStorageUtils.getJson(storageKey.sessionStorageKey.schoolMap);

//表格初始化配置
var tableInit = function () {
    var columns = [
        {
            title: '全选',
            field: 'select',
            //复选框
            checkbox: true,
            width: 25,
            align: 'center',
            valign: 'middle'
        },
        {
            field: 'trackName',
            title: '跟踪人姓名',
            align: 'center',
            valign: 'middle',
        },
        {
            field: 'timesNum',
            title: '次数',
            align: 'center',
            valign: 'middle',
            formatter:function(value,row,index){
                return "第"+value+"次";
            }
        },
        {
            field: 'name',
            title: '客户姓名',
            align: 'center',
            valign: 'middle',
        },
        {
            field: 'nickname',
            title: '客户昵称',
            align: 'center',
            valign: 'middle',
        },
        {
            field: 'imgUrl',
            title: '头像',
            align: 'center',
            valign: 'middle',
            formatter:function(value,row,index){
                var url = "";
                if (!empty(value)) {
                    url = value;
                } else {
                    url = row.headImg;
                }
                return "<a href='" + url + "' title='点击查看大图' target='_blank'>" +
                    "<img src='" + url + "' width='40' onerror='imgNotfind(event);' /></a>";
            }
        },
        {
            field: 'sex',
            title: '性别',
            align: 'center',
            valign: 'middle',
        },
        {
            field: 'wx_tel',
            title: '微信绑定手机号',
            align: 'center',
            valign: 'middle',
        },{
            field: 'phone',
            title: '常用手机号',
            align: 'center',
            valign: 'middle',
        },
        {
            field: 'trackTime',
            title: '回访时间',
            align: 'center',
            valign: 'middle',
            formatter:function(value,row,index){
                if (empty(value)) {
                    return "-";
                }
                return formatDateTime(value);
            }
        },
        {
            field: 'gmt_create',
            title: '创建时间',
            align: 'center',
            visible: false,
            formatter:function(value,row,index){
                if (empty(value)) {
                    return "-";
                }
                return formatDateTime(value);
            }
        },
    ];

    //请求服务数据时所传参数
    function queryParams(params) {
        return {
            pageNo: params.pageNumber,
            pageSize: params.pageSize,
            companyId:companyId,
            // schoolId:schoolId,
            searchText: params.searchText,
            sortName: params.sortName,      //排序列名
            sortOrder: params.sortOrder //排位命令（desc，asc）
        }
    }

    var tableConfig = $.extend(bootstrapTableConfig(_HOST + _API.user_orderTrackRelation_getTodayMemberList, queryParams, columns), {
        //这里是其他的一些个性化的 bootstrap-table options
        //例如 覆盖一些公共的配置
        //例如 添加一些私有的配置
        showColumns: true,//列显示操作按钮
        ajaxOptions: { //设置自定义请求头
            headers: {
                'Authorization': sessionStorageUtils.getJson(storageKey.sessionStorageKey.token),
                'permCoding': ''
            }
        },
        //例如格式化数据
        responseHandler: function (res) {
            //在ajax获取到数据，渲染表格之前，修改数据源
            //console.log(res)
            if (res.success) {
                //更新本地token
                sessionStorageUtils.setJsonStr(storageKey.sessionStorageKey.token, res.token);
            }
            return res;
        }
    });
    //调用实例化
    $('#bootstrapTableEvents').bootstrapTable(tableConfig);
    $(".search input").attr('placeholder', '输入顾客姓名电话，跟踪人姓名搜索');
};

var app = new Vue({
    el: '#app',
    data: {
        permissions: ['visit:deal'],//本页权限数组
    },
    //进入页面初始化方法
    created: function () {
        var that = this;
        var token = sessionStorageUtils.getJson(storageKey.sessionStorageKey.token);
        if (empty(token)) {
            layer.msg("<span style='color:red'>登录超时！请重新登录！</span>", {time: 1500, icon: 5});
            setTimeout(function () {
                window.location.href = _LOGIN_PAGE;
            }, 1200);

            return;
        }
    },
    methods: {
        //权限控制
        permission: function (index) {
            return (_PERMISSIONS.indexOf(this.permissions[index]) != -1);
        },
        lookVisitRecord: function () {
            var that = this;
            var $result = $('#bootstrapTableEvents');
            var list = $result.bootstrapTable('getSelections');
            if (list.length <= 0 || list.length > 1) {
                layer.msg('请选中一条数据', {time: 1500, icon: 5});
                return;
            }
            list = list[0];

            var index = layer.open({
                type: 2,
                title: "修改",
                content: "../visit/record.html?id=" + list.orderInfoId+"&relationId="+list.relationId,
                fix: true,
                shadeClose: true,
                shade: false,
                area: ['70%', '70%'],
                maxmin: true, //开启最大化最小化按钮
            });
            layer.full(index);
        },
    },
});

$(function () {
    //初始化表格
    tableInit();
});
